package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUserSalaryHistory;
import com.ruoyi.system.mapper.SysUserSalaryHistoryMapper;
import com.ruoyi.system.service.ISysUserSalaryHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysUserSalaryHistoryServiceImpl implements ISysUserSalaryHistoryService {
    @Autowired
    private SysUserSalaryHistoryMapper sysUserSalaryHistoryMapper;

    @Override
    public int insertUserSalaryHistoryByUserId(Long payId, Long userId, Date nowDate) {
        return sysUserSalaryHistoryMapper.insertUserSalaryHistoryByUserId(payId, userId, nowDate);
    }

    @Override
    public List<SysUserSalaryHistory> getSalaryHistoryListByNowDate(Date nowDate) {
        return sysUserSalaryHistoryMapper.getSalaryHistoryListByNowDate(DateUtils.parseDateToStr("yyyy-MM", nowDate));
    }

    @Override
    public SysUserSalaryHistory getSalaryHistoryByUserIdAndNowDate(Long userId, Date nowDate) {
        return sysUserSalaryHistoryMapper.getSalaryHistoryByUserIdAndNowDate(userId, DateUtils.parseDateToStr("yyyy-MM", nowDate));
    }

    @Override
    public int insertOrUpdateUserSalaryHistoryByUserId(Long payId, Long userId, Date nowDate) {
        String nowDateStr = DateUtils.parseDateToStr("yyyy-MM", nowDate);
        int num = 0;
        if (getSalaryHistoryByUserIdAndNowDate(userId, nowDate) == null) {
            //历史表没有当月数据时直接插入
            num = insertUserSalaryHistoryByUserId(payId, userId, nowDate);
        } else {
            //有当月数据时更新
            num = sysUserSalaryHistoryMapper.updateUserSalaryHistoryByUserId(payId, userId, nowDate, nowDateStr);
        }
        return num;
    }
}
